﻿<cfsilent>
	<cfscript>
		
		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		dictAdvice = getProperty("serviceFactory").getBean("senateDictionaryAdvice");
		markAdvice = getProperty("serviceFactory").getBean("markAdvice");
		
		teacherId = sessionAdvice.getAutherUserID();
		
		hashCode = event.getArg("Sign");
		taskId = event.getArg("TSK");
		taskType = event.getArg("TT");
		
		checked = false;
			
			if ( decrypt(hashCode, teacherId, "PBEWithMD5AndDES", "Hex") eq taskId ) {
				
				checked = true;
				
				/* 载入期末成绩提交任务 */
				sql = "SELECT
						   c.course_name,
						   ttm.cidx,
						   tt.academic_year,
						   tt.academic_term
						FROM
							t_task_mark ttm
							INNER JOIN t_course c ON c.cid = ttm.cid
							INNER JOIN t_term tt ON tt.term_id = ttm.term_id
						WHERE
							ttm.task_id = :taskId ";

				queryObj = new Query(datasource = application.dnsSlave);
				
				queryObj.addParam(name ="taskId", value=taskId, cfsqltype="cf_sql_char");
				
				rs_task = queryObj.execute(sql = sql).getResult();
				
				
				/* 载入成绩提交名单 */

				sql = "	SELECT tm.stu_id,
							stu.stu_name,
							clz.cls_name,
						   	clz.grade,
						   	sbj.sbj_name,
						   	i.institute_name,
						   	tm.st,
						   	tm.retaking,
							tm.mark_mode,
							tm.mark,
							tm.mark_note
						FROM t_temp_mark tm
							INNER JOIN t_student stu ON stu.stu_id = tm.stu_id
							INNER JOIN t_class clz ON stu.cls_id = clz.cls_id
							INNER JOIN t_subject sbj ON sbj.sbj_id = clz.sbj_id
							INNER JOIN t_institute i ON i.institute_id = sbj.institute_id
							INNER JOIN t_course c ON c.cid = tm.cid
					 	WHERE 
							tm.task_id = :taskId 
						ORDER BY 
							tm.stu_id";

				queryObj = new Query(datasource = application.dnsSlave);
				
				queryObj.addParam(name="taskId", value=taskId, cfsqltype="cf_sql_char");
				
				rs_chooser = queryObj.execute(sql=sql).getResult();
				
				temFile = GetTempDirectory() & createUUID() & ".xls";
				downFile = URLEncodedFormat(rs_task.course_name & "," & rs_task.cidx & "," & rs_task.academic_year & "-" & rs_task.academic_term & ",成绩录入模版", "utf-8");
				
				excel = getProperty("serviceFactory").getBean("spreadSheetObject");
		
				/* 创建工作簿 */
				spreadsheetObj = excel.SpreadSheetNew("成绩名单", false);
				
				/* 添加表头 */
				excel.SpreadsheetAddrow(spreadsheetObj, "学号,姓名,班级,年级,专业,学院,修读方式,是否重修,考核成绩");
				
				row = 1;
		
				for(a = 1; a lte rs_chooser.recordCount; a++){
					row++;
			
					/* 构建选课数据 */
					rowData = rs_chooser['stu_id'][a]
							& "," & rs_chooser['stu_name'][a]
							& "," & rs_chooser['cls_name'][a]
							& "," & rs_chooser['grade'][a]
							& "," & rs_chooser['sbj_name'][a]
							& "," & rs_chooser['institute_name'][a]
							& "," & dictAdvice.getStudyType(rs_chooser['st'][a])
							& "," & dictAdvice.getBoolean(rs_chooser['retaking'][a])
							& "," & markAdvice.getMark( rs_chooser['mark_mode'][a], rs_chooser['mark'][a], rs_chooser['mark_note'][a] );
			
					/* 写入选课信息 */
					excel.SpreadsheetAddrow(spreadsheetObj, rowData);
				}
		
				excel.SpreadSheetWrite(spreadsheetObj, temFile, true);
				
			}

	
	</cfscript>
	
	<cfif checked >
		<cfheader name="Content-Disposition" value="attachment; filename=#downFile#.xls" />
		<cfcontent file="#temFile#" reset="yes" type="application/msexcel" deletefile="yes" />	
	</cfif>	
	
</cfsilent>